package com.ruoyi.module.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.page.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.module.app.domain.Subject;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.module.app.service.ISubjectService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 课程Controller
 *
 * @author mars
 * @date 2023-03-09
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/app/subject" )
public class SubjectController extends BaseController {

    private final ISubjectService iSubjectService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('app:subject:list')")
    @GetMapping("/list")
    public PageInfo list(Subject subject) {
        return iSubjectService.findPageBySpec(subject);
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:subject:query')" )
    @GetMapping(value = "/query/{id}" )
    public Result getInfo(@PathVariable("id" ) Integer id) {
        return Result.success(iSubjectService.getById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('app:subject:add')" )
    @Log(title = "课程" , businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody Subject subject) {
        return Result.success("新增成功",iSubjectService.add(subject));
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('app:subject:edit')" )
    @Log(title = "课程" , businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody Subject subject) {
        return toAjax(iSubjectService.update(subject) ? 1 : 0);
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('app:subject:remove')" )
    @Log(title = "课程" , businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}" )
    public Result remove(@PathVariable Integer[] ids) {
        return toAjax(iSubjectService.deleteBatch(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('app:subject:export')" )
    @Log(title = "课程" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public Result export(Subject subject) {
        LambdaQueryWrapper<Subject> lqw = new LambdaQueryWrapper<Subject>(subject);
        List<Subject> list = iSubjectService.list(lqw);
        ExcelUtil<Subject> util = new ExcelUtil<Subject>(Subject. class);
        return util.exportExcel(list, "subject" );
    }
}
